package com.leo;

import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

/**
 * @ClassName TranslateDemo
 * @Description 位移
 * @Author wangss
 * @date 2020.06.27 22:28
 * @Version 1.0
 */
public class TranslateDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        Button b1 = new Button("b1");
        b1.setPrefWidth(100d);
        b1.setPrefHeight(100d);
        b1.setLayoutX(100d);
        b1.setLayoutY(100d);

        Button b2 = new Button("b2");
        b2.setPrefWidth(100d);
        b2.setPrefHeight(100d);

        Translate translate = new Translate(200, 200);
        b2.getTransforms().add(translate);

        AnchorPane an = new AnchorPane();
        an.setPrefWidth(400d);
        an.setPrefHeight(400d);
        an.setStyle("-fx-background-color: #eea5c4");
        an.getChildren().addAll(b1, b2);

        AnchorPane root = new AnchorPane();
        root.getChildren().add(an);
        AnchorPane.setTopAnchor(an, 100d);
        AnchorPane.setLeftAnchor(an, 100d);

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setTitle("位移");
        primaryStage.setHeight(800d);
        primaryStage.setWidth(800d);
        primaryStage.show();

        System.out.println(b2.getLayoutX() + " - " + b2.getLayoutY());
        System.out.println(b2.getLocalToParentTransform().getTx() + " - " + b2.getLocalToParentTransform().getTy());
        System.out.println(b2.getLocalToSceneTransform().getTx() + " - " + b2.getLocalToSceneTransform().getTy());

        // 坐标转换
        Bounds layoutBounds = b2.getLayoutBounds();
        Bounds bounds = b2.localToParent(layoutBounds);
        System.out.println(bounds.getMinX() + " - " + bounds.getMinY());

        Bounds bounds1 = b2.localToScene(layoutBounds);
        System.out.println(bounds1.getMinX() + " - " + bounds1.getMinY());
    }
}
